package io.github.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import io.github.annotation.Log;
import io.github.entity.SysGatewayEntity;
import io.github.service.SysGatewayService;
import io.github.service.impl.SysGatewayServiceImpl;
import io.github.util.PageUtils;
import io.github.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/sys/gateway")
public class SysGatewayController extends AbstractController {

    @Autowired
    private SysGatewayService sysGatewayService;

    @RequestMapping("/list")
    @Log(value = "网关配置查询",action = 0)
    public R list(@RequestBody JSONObject jsonObject){
//        Integer offset = jsonObject.getInteger("offset");
//        Integer limit = jsonObject.getInteger("limit");

        String name = jsonObject.getString("name");
        List<SysGatewayEntity> list = sysGatewayService.queryListByPage(name);

        //PageUtils pageUtils = new PageUtils(list.getRecords(),list.getTotal(),list.getSize(),list.getCurrent());
        return R.ok().put("list",list);
    }

    @Log(value = "网关配置添加",action = 1)
    @RequestMapping("/add")
    public R save(@RequestBody SysGatewayEntity entity){
        sysGatewayService.save(entity);
        return R.ok();
    }

    @Log(value = "网关配置删除",action = 3)
    @RequestMapping("/delete")
    public R delete(@RequestBody JSONObject jsonObject){
        String ids = jsonObject.getString("ids");
        String[] id = ids.split(",");
        if(id.length<1){
            return R.error(500,"请选择要删除的数据");
        }
        sysGatewayService.deleteBatch(id);
        return R.ok();
    }

    @Log(value = "网关配置修改",action = 2)
    @RequestMapping("/update")
    public R update(@RequestBody SysGatewayEntity entity){
        Integer id = entity.getId();
        sysGatewayService.update(entity,id);
        return R.ok();
    }
}
